package com.liuyu.common.framework.common.utils;

import org.apache.commons.lang3.StringUtils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * @ClassName ImageUtils
 * @Author: liuyu
 * @Date: Created in 2021/7/5 15:03
 * @Version: 1
 * @Modified By:
 */
public class ImageUtils {

    private static final int INT_10 = 10;

    /**
     * @param path 源路径
     * @param tarPath 生成路径
     * @param alpha    透明度   （0不透明---10全透明）
     */
    public static void changeAlpha(String path, String tarPath, int alpha) {
        //检查透明度是否越界
        if (alpha < 0) {
            alpha = 0;
        } else if (alpha > INT_10) {
            alpha = 10;
        }
        try {
            BufferedImage image = ImageIO.read(new File(path));
            int weight = image.getWidth();
            int height = image.getHeight();

            BufferedImage output = new BufferedImage(weight, height, BufferedImage.TYPE_INT_ARGB);
            Graphics2D g2 = output.createGraphics();
            output = g2.getDeviceConfiguration().createCompatibleImage(weight, height, Transparency.TRANSLUCENT);
            g2.dispose();
            g2 = output.createGraphics();

            //调制透明度
            for (int j1 = output.getMinY(); j1 < output.getHeight(); j1++) {
                for (int j2 = output.getMinX(); j2 < output.getWidth(); j2++) {
                    int rgb = output.getRGB(j2, j1);
                    rgb = ((alpha * 255 / 10) << 24) | (rgb & 0x00ffffff);
                    output.setRGB(j2, j1, rgb);
                }
            }
            g2.setComposite(AlphaComposite.SrcIn);
            g2.drawImage(image, 0, 0, weight, height, null);
            g2.dispose();
            ImageIO.write(output, "png", new File(tarPath));

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static void removeBackground(String path, String target, String removeRgb) throws IOException {
        BufferedImage bufferedImage = ImageIO.read(new File(path));
        int alpha = 255;
        // 遍历Y轴的像素
        for (int y = bufferedImage.getMinY(); y < bufferedImage.getHeight(); y++) {
            // 遍历X轴的像素
            for (int x = bufferedImage.getMinX(); x < bufferedImage.getWidth(); x++) {
                int rgb = bufferedImage.getRGB(x, y);
                // 取图片边缘颜色作为对比对象
                if (StringUtils.isBlank(removeRgb)) {
                    if (y == bufferedImage.getMinY() && x == bufferedImage.getMinX()) {
                        removeRgb = convertRgb(rgb);
                    }
                }
                // 设置为透明背景
                if (removeRgb.equals(convertRgb(rgb))) {
                    alpha = 0;
                } else {
                    alpha = 255;
                }
                rgb = (alpha << 24) | (rgb & 0x00ffffff);
                bufferedImage.setRGB(x, y, rgb);
            }
        }
        ImageIO.write(bufferedImage, "png", new File(target));
    }

    public static String convertRgb(int color) {
        // 获取color(RGB)中R位
        int red = (color & 0xff0000) >> 16;
        // 获取color(RGB)中G位
        int green = (color & 0x00ff00) >> 8;
        // 获取color(RGB)中B位
        int blue = (color & 0x0000ff);
        return red + "," + green + "," + blue;
    }
}
